//
//  StarViewController.m
//  frameWork
//
//  Created by mac on 16/4/21.
//  Copyright © 2016年 TLiang. All rights reserved.
//

#import "TLMainViewController.h"
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface TLMainViewController ()
@property(nonatomic,retain)UIImageView *myImageView;
@property(nonatomic,strong)MPMoviePlayerController *moviePlayer;
@property(nonatomic ,strong)NSTimer *timer;
@property(nonatomic,strong)UIScrollView *MyscrollView;

@end

@implementation TLMainViewController
-(void)viewWillAppear:(BOOL)animated{

    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.alpha = 1;
    self.view.backgroundColor = [UIColor whiteColor];

    [self playMovie];
    
    
    // Do any additional setup after loading the view.
}

-(void)playMovie{
   
     NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"家范儿" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    _moviePlayer.view.frame = [[UIScreen mainScreen]bounds];
    //  _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    [_moviePlayer play];
    _moviePlayer.view.backgroundColor = [UIColor whiteColor];
    
    [_moviePlayer.view setFrame:self.view.bounds];
    
    [self.view addSubview:_moviePlayer.view];
    _moviePlayer.shouldAutoplay = YES;
    [_moviePlayer setControlStyle:MPMovieControlStyleNone];
    [_moviePlayer setFullscreen:YES];
    
    [_moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateChanged)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:_moviePlayer];
    
    UILabel *nameL = [[UILabel alloc]initWithFrame:CGRectMake(0,120, KScreenWidth, 40)];
    [self.view addSubview:nameL];
    nameL.text = @"iHouse";
    nameL.textColor = [UIColor whiteColor];
    nameL.font = [UIFont systemFontOfSize:30];
    nameL.textAlignment = NSTextAlignmentCenter;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.layer.masksToBounds = YES;
    leftButton.layer.cornerRadius = 5;
    leftButton.backgroundColor = RED;
    [leftButton setTitle:@"登陆" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    leftButton.alpha = 0.9;
    leftButton.tag = 10000;
    [leftButton addTarget:self action:@selector(seeAll:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(15);
        make.bottom.equalTo(self.view).offset(-15);
        make.size.mas_offset(CGSizeMake((KScreenWidth - 45)/2, 50.00));
        
    }];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.tag = 10001;
    [rightButton addTarget:self action:@selector(seeAll:) forControlEvents:UIControlEventTouchUpInside];
    rightButton.layer.masksToBounds = YES;
    rightButton.layer.cornerRadius = 5;
    rightButton.backgroundColor = [UIColor grayColor];
    [rightButton setTitle:@"注册" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.alpha = 0.9;
    [self.view addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view).offset(-15);
        make.bottom.equalTo(self.view).offset(-15);
        make.size.mas_offset(CGSizeMake((KScreenWidth - 45)/2, 50.00));
        
    }];
    
    UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    downButton.tag = 10002;
    [downButton setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
    [downButton addTarget:self action:@selector(seeAll:) forControlEvents:UIControlEventTouchUpInside];
    //    downButton.layer.masksToBounds = YES;
    //    downButton.layer.cornerRadius = 15;
    downButton.backgroundColor = [UIColor clearColor];
    //    downButton.alpha = 0.6;
    [self.view addSubview:downButton];
    [downButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(self.view).offset(15);
        make.size.mas_offset(CGSizeMake(43,32));
        
    }];
    
    [self addScrollViewDtail];
    
    
}
-(void)playbackStateChanged{
    
    
    //取得目前状态
    MPMoviePlaybackState playbackState = [_moviePlayer playbackState];
    
    //状态类型
    switch (playbackState) {
        case MPMoviePlaybackStateStopped:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStatePlaying:
            NSLog(@"播放中");
            break;
            
        case MPMoviePlaybackStatePaused:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStateInterrupted:
            NSLog(@"播放被中断");
            break;
            
        case MPMoviePlaybackStateSeekingForward:
            NSLog(@"往前快转");
            break;
            
        case MPMoviePlaybackStateSeekingBackward:
            NSLog(@"往后快转");
            break;
            
        default:
            NSLog(@"无法辨识的状态");
            break;
    }
}
-(void)viewDidDisappear:(BOOL)animated{
    [_moviePlayer stop];
    
}
-(void)addScrollViewDtail{
    
    self.MyscrollView = [[UIScrollView alloc]init];
    [self.view addSubview:_MyscrollView];
    self.MyscrollView.backgroundColor = [UIColor clearColor];
    _MyscrollView.contentSize = CGSizeMake(KScreenWidth * 3, 0);
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
    [self.MyscrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-50.00 - 40.00);
        make.size.mas_offset(CGSizeMake(KScreenWidth, 30));
        
    }];
    NSArray *nnn = [NSArray arrayWithObjects:@"iHouse，Believe in yourself.",@"Judge not from appearances.",@"The world is his who enjoys it.", nil];
    for (int i = 0; i < 3; i ++) {
        UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(i*KScreenWidth, 0, KScreenWidth, 30)];
        l.textAlignment = NSTextAlignmentCenter;
        l.textColor = [UIColor whiteColor];
        [self.MyscrollView addSubview:l];
        l.backgroundColor = [UIColor clearColor];
        //        [l mas_makeConstraints:^(MASConstraintMaker *make) {
        //
        //            make.left.equalTo(self.MyscrollView).offset(i *kScreenWidth);
        //            make.size.mas_offset(CGSizeMake(kScreenWidth, 30));
        //
        //        }];
        l.text = [nnn objectAtIndex:i];
    }
    
    
    
}
-(void)timer:(NSTimer *)timer{
    
    self.MyscrollView.contentOffset = CGPointMake(self.MyscrollView.contentOffset.x +KScreenWidth, 0);
    if (self.MyscrollView.contentOffset.x == KScreenWidth * 3) {
        self.MyscrollView.contentOffset = CGPointMake(0, 0);
    }
   
    
}
-(void)seeAll:(UIButton *)logOrSee{
    
    if (logOrSee.tag == 10000) {
        NSLog(@"登录");
    }
    else if(logOrSee.tag == 10001){
        NSLog(@"预览");
        
        
    }
    else if(logOrSee.tag == 10002){
        
        AppDelegate *app = [[UIApplication sharedApplication]delegate];

        //        [_moviePlayer stop];
    }
}


-(void)movieFinishedCallBack:(NSNotification*)notiyf{
    
    NSLog(@"ddd");
    AppDelegate *app = [[UIApplication sharedApplication]delegate];
   
    
}






-(void)TimerAction:(NSTimer *)timer{
    
    [UIView animateWithDuration:1.0 animations:^{
        self.myImageView =[[ UIImageView alloc]initWithFrame:CGRectMake(-20, 0, KScreenWidth, KScreenHeight)];
        self.myImageView.center = CGPointMake(KScreenWidth/2, KScreenHeight/2);
        self.myImageView.alpha = 0.618;
        NSString *picture = @"qqidong.png";
        self.myImageView.image = [UIImage imageNamed:picture];
        
        self.myImageView.center = CGPointMake(KScreenWidth/2, KScreenHeight/2);
        [self.view addSubview:self.myImageView];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *name =@"ihouse";
        [defaults setObject:name forKey:@"user"];
        
    } completion:^(BOOL finished) {
        
        AppDelegate *app = [[UIApplication sharedApplication]delegate];
//        [app goMain];
        
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
